import React from 'react';
import { inject, observer } from 'mobx-react';
import { toJS } from 'mobx';
import { WeaTools, WeaLocaleProvider, WeaTop, WeaTab, WeaLeftRightLayout, WeaLeftTree, WeaFormItem, WeaSearchGroup, WeaRightMenu, WeaErrorPage } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;
import { WeaTableNew, WeaSwitch } from 'comsMobx';

const WeaTable = WeaTableNew.WeaTable;
import { Button, Form, message } from 'antd';


@inject('highSeasLogListStore')
@observer
class HighSeasLogList extends React.Component {
  constructor(props) {
    super(props);
  }

  componentDidMount() {
    const { highSeasLogListStore, logType, logDialogSeasId } = this.props;
    highSeasLogListStore.initDatas({ operateType: logType, seasId: logDialogSeasId });
  }

  render() {
    const { highSeasLogListStore } = this.props;
    const { tableStore, loading, showSearchAd, searchConditionForm } = highSeasLogListStore;
    return (
      <div>
        <WeaTab
          ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@ufoyw9`}
          searchType={['advanced']}
          keyParam="viewcondition" // 主键
          // searchsBaseValue={searchKey}
          // onSearchChange={v => highSeasLogListStore.setSearchKey(v)}
          showSearchAd={showSearchAd}
          buttonsAd={this.getAdButtons(highSeasLogListStore)}
          setShowSearchAd={bool => highSeasLogListStore.setShowSearchAd(bool)}
          hideSearchAd={() => highSeasLogListStore.setShowSearchAd(false)}
          searchsAd={<Form ecId={`${this && this.props && this.props.ecId || ''}_Form@ar04p6`} horizontal>{this.getSearchConditionForm(highSeasLogListStore)}</Form>}
          onSearch={highSeasLogListStore.doSearch}
        />
        <WeaTable
          ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@j2wk2s`}
          comsWeaTableStore={tableStore}
          hasOrder
          needScroll
          loading={loading}
        />
      </div>
    );
  }

  getRightMenus() {
    const { highSeasLogListStore } = this.props;
    const { rightMenu, tableStore } = highSeasLogListStore;
    const { selectedRowKeys } = tableStore;
    const isDisabled = !(selectedRowKeys.length > 0 && `${toJS(selectedRowKeys)}`);
    const btnArr = [];
    rightMenu && rightMenu.map((m) => {
      btnArr.push({
        icon: <i className={m.menuIcon} />,
        content: m.menuName,
        disabled: (m.isControl == '1' && isDisabled),
      });
    });
    return btnArr;
  }

  getAdButtons(store) {
    const { doSearch, setShowSearchAd, clearAdFields } = store;
    return [
      (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@72nj9w@search`} type="primary" onClick={() => { doSearch(); setShowSearchAd(false); }}>搜索</Button>),
      (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@td8y5f@reset`} type="ghost" onClick={() => { clearAdFields(); }}>重置</Button>),
      (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@97za4w@cancel`} type="ghost" onClick={() => { setShowSearchAd(false); }}>取消</Button>),
    ];
  }

  getSearchConditionForm(store) {
    const { searchConditionComponents, searchConditionForm } = store;
    const groups = [];
    if (searchConditionComponents) {
      const { isFormInit } = searchConditionForm;
      const formParams = searchConditionForm.getFormParams();
      isFormInit && toJS(searchConditionComponents).map((c,indexGroup) => {
        const items = [];
        c.items.map((fields, index) => {
          items.push({
            com: (<WeaFormItem
              ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@agwxvm@${index}`}
              label={`${fields.label}`}
              labelCol={{ span: `${fields.labelcol}` }}
              wrapperCol={{ span: `${fields.fieldcol}` }}
            >
              <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@yymedp@${index}`} fieldConfig={fields} form={searchConditionForm} formParams={formParams} onChange={store.searchConditionFieldChange} onEnterSearch={this.onEnterSearch} />
            </WeaFormItem>),
            col: 2,
          });
        });
        groups.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@nc2pyk@${indexGroup}`} needTigger title={c.title} showGroup={c.defaultshow} items={items} />);
      });
    }
    return groups;
  }

  onEnterSearch=() => {
    const { highSeasLogListStore } = this.props;
    highSeasLogListStore.doSearch();
  }
}


export default WeaTools.tryCatch(React,
  props => <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@m6vh3m`} msg={props.error ? props.error : getLabel(129417, '服务器运行出错，请联系系统管理员！')} />,
  { error: '' },
)(HighSeasLogList);
